//
//  StringExtension.swift
//  HFUTER
//
//  Created by HD on 15/1/28.
//  Copyright (c) 2015年 HD. All rights reserved.
//

import UIKit

extension String {
    
    ///分割字符
    func split(s: String)->[String]{
        if s.isEmpty{
            var x=[String]()
            for y in self{
                x.append(String(y))
            }
            return x
        }
        return self.componentsSeparatedByString(s)
    }
    
    ///去掉左右空格
    func trim() -> String{
        return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
    }
    
    func removeBlank() -> String {
        return self.replace("\n", to: "").replace(" ", to: "").replace("\t", to: "")
    }
    
    ///替换字符串
    func replace(from: String, to: String) -> String {
        return self.stringByReplacingOccurrencesOfString(from, withString: to, options: NSStringCompareOptions.LiteralSearch, range: nil)
    }
    
    ///重复字符串
    func repeat(times: Int) -> String {
        var result = ""
        for i in 0 ..< times {
            result += self
        }
        return result
    }
    
    ///反转
    func reverse() -> String {
        var s = self.split("").reverse()
        var x = ""
        for y in s {
            x += y
        }
        return x
    }
    
    ///截取字符串，一直到toIndex
    func subStringToIndex(toIndex: Int) -> String {
        return (self as NSString).substringToIndex(toIndex)
    }
    
    ///截取字符串，从fromIndex开始
    func subStringFromIndex(fromIndex: Int) -> String {
        return (self as NSString).substringFromIndex(fromIndex)
    }
    
    ///截取字符串，从from到to
    func subString(from: Int, to: Int) -> String {
        let startIndex = advance(self.startIndex, 0)
        let endIndex = advance(self.endIndex, self.length - 1)
        var range = Range<String.Index>(start: startIndex, end: endIndex)
        return substringWithRange(range)
    }
    
    ///字符串长度
    var length: Int! {
        get {
            return count(self)
        }
    }
    
    ///是否包含字符串
    func has(str: String) -> Bool{
        if self.rangeOfString(str) != nil {
            return true
        } else {
            return false
        }
    }
    
    ///是否包含前缀
    func hasBegin(s:String) -> Bool{
        if self.hasPrefix(s) {
            return true
        }else{
            return false
        }
    }
    
    ///是否包含后缀
    func hasEnd(s: String) -> Bool{
        if self.hasSuffix(s) {
            return true
        }else{
            return false
        }
    }
    
    ///拼音
    func pinyin() -> String {
        var ms = NSMutableString(string: self)
        var range = CFRange(location: 0, length: self.length)
        withUnsafeMutablePointer(&range, { (p1: UnsafeMutablePointer) -> Void in
            CFStringTransform(ms, p1, kCFStringTransformMandarinLatin, 0)
            CFStringTransform(ms, p1, kCFStringTransformStripDiacritics, 0)
            return
        })
        return ms as String
    }
    
    ///大写字母开头
    func startWithUppercaseString() -> String {
        return subStringToIndex(1).uppercaseString + subStringFromIndex(1)
    }
    
    ///获取字符串高度
    func stringHeightWith(fontSize: CGFloat, width: CGFloat) -> CGFloat {
        var font = UIFont.systemFontOfSize(fontSize)
        var size = CGSizeMake(width,CGFloat.max)
        var paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineBreakMode = .ByWordWrapping;
        var attributes = [NSFontAttributeName: font, NSParagraphStyleAttributeName: paragraphStyle.copy()]
        
        var text = self as NSString
        var rect = text.boundingRectWithSize(size, options: .UsesLineFragmentOrigin, attributes:  attributes, context:nil)
        return rect.size.height
    }
    
    ///转换成NSData
    func toData() -> NSData {
        return toData(NSUnicodeStringEncoding)
    }
    
    ///转换成NSData
    ///encoding 编码方式
    func toData(encoding: UInt) -> NSData {
        return (self as NSString).dataUsingEncoding(encoding)!
    }
}
